package heap

// IsAssignableFrom 一个类是否是另一个类的父类或接口。它主要用于类的继承关系和接口实现关系的检查
func (e *Class) IsAssignableFrom(other *Class) bool {
	s, t := other, e

	if s == t {
		return true
	}

	if !s.IsArray() {
		if !s.IsInterface() {
			if !t.IsInterface() {
				return s.IsSubClassOf(t)
			} else {
				return s.IsImplements(t)
			}
		} else {
			if !t.IsInterface() {
				return t.isJ1Object()
			} else {
				return t.isSuperInterfaceOf(s)
			}
		}
	} else {
		if !t.IsArray() {
			if !t.IsInterface() {
				return t.isJ1Object()
			} else {
				return t.isJlCloneable() || t.isJioSerializable()
			}
		} else {
			sc := s.ComponentClass()
			tc := t.ComponentClass()
			return sc == tc || tc.IsAssignableFrom(sc)
		}
	}
}

// IsSubClassOf e是否继承c
func (e *Class) IsSubClassOf(other *Class) bool {
	for c := e.superClass; c != nil; c = c.superClass {
		if c == other {
			return true
		}
	}
	return false
}

// IsImplements 是否是接口
func (e *Class) IsImplements(other *Class) bool {
	for c := e; c != nil; c = c.superClass {
		for _, i := range c.interfaces {
			if i == other || i.IsSubClassOf(other) {
				return true
			}
		}
	}
	return false
}

// self extends iface
func (e *Class) isSubInterfaceOf(iface *Class) bool {
	for _, superInterface := range e.interfaces {
		if superInterface == iface || superInterface.isSubInterfaceOf(iface) {
			return true
		}
	}
	return false
}

func (e *Class) IsSuperClassOf(other *Class) bool {
	return other.IsSubClassOf(e)
}

func (e *Class) isSuperInterfaceOf(iface *Class) bool {
	return iface.IsSubClassOf(e)
}
